package com.iretailer.report.web.business;

import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.ReZoneCamera;
import com.iretailer.report.model.*;
import com.iretailer.report.util.RMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: xjw
 * @Date: 2019/1/25 15:17
 * @Description:
 */
@RestController
public class ZoneCameraController extends BaseAction {

    @RequestMapping("/business/getCameraZone.action")
    public Map<String, Object> getCameraZone(@RequestBody ReZoneCamera rzc) {
        List<CameraZoneCamera> allCameraZone = cameraZoneService.selectCameraZoneByEnabled_MallId(null, rzc.getMallId());
        List<CameraZoneCamera> search = cameraZoneService.selectCameraZoneByEnabled_PhysicalZoneId(null, rzc.getSearchId());
        Map<Long, CameraZone> searchMap = search.stream().collect(Collectors.toMap(CameraZone::getId, a -> a));
        List<CameraZoneCamera> all = allCameraZone.stream().filter(it -> searchMap.get(it.getId()) == null).collect(Collectors.toList());
        return returnResult(all, search);
    }

    @RequestMapping("/business/getPhysicalByCamera.action")
    public Map<String, Object> getPhysicalByCamera(@RequestBody ReZoneCamera rzc) {
        List<PhysicalZoneMall> allPhysicalZones = physicalzonesService.selectByEnabled_MallId(rzc.getEnable(), rzc.getMallId());
        List<PhysicalZoneMall> search = physicalzonesService.selectPhysicalZonesByEnabled_CameraZoneId(rzc.getEnable(), rzc.getSearchId());
        Map<Long, PhysicalZoneMall> searchMap = search.stream().collect(Collectors.toMap(PhysicalZoneMall::getId, a -> a));
        List<PhysicalZoneMall> all = allPhysicalZones.stream().filter(it -> searchMap.get(it.getId()) == null).collect(Collectors.toList());
        return returnResult(all, search);
    }

    @RequestMapping("/business/connectByPhysical.action")
    public Map<String, Object> connectByPhysical(@RequestBody ReZoneCamera rzc) {
        boolean result = physicalzonesService.connectByPhysical(rzc.getPhysicalZoneId(), rzc.getCameraZoneCameras());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/connectByCamera.action")
    public Map<String, Object> connectByCamera(@RequestBody ReZoneCamera rzc) {
        boolean result = physicalzonesService.connectByCamera(rzc.getCameraZoneId(),rzc.getPhysicalZones());
        return RMap.resultBoolean(result);
    }

    private Map<String, Object> returnResult(List all, List search) {
        Map<String, Object> result = new HashMap<>();
        result.put("All", all);
        result.put("Search", search);
        return RMap.ok(result);
    }
}
